package org.tulkas.web;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.view.velocity.VelocityView;
import org.tulkas.base.Constants;
import org.tulkas.util.HttpUtil;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class TulkasTemplateView extends VelocityView {
	private void setupRootUrl(Map<String, Object> model,
			HttpServletRequest request) {
		String url = request.getRequestURL().toString();
		String rootUrl = HttpUtil.getRootUrl(url);
		String cxtPath = request.getContextPath();
		if (cxtPath != null && !"/".equals(cxtPath)) {
			rootUrl += cxtPath;
		}

		model.put(Constants.COMMON_ATTR_ROOTURL, rootUrl);
	}

	@Override
	public boolean checkResource(Locale locale) throws Exception {
		return true;
	}

	@Override
	protected void exposeHelpers(Map<String, Object> model,
			HttpServletRequest request) throws Exception {
		super.exposeHelpers(model, request);
		setupRootUrl(model, request);
	}
}
